package com.cdj.common.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;


/**
 * ---------------------------------------------------------<br />
 * desc：<br />
 * author：chendeji <br />
 * date：2021/6/26<br />
 * email：18701434169@163.com<br />
 * qq: 781571323
 * wx: melody_2009
 * ---------------------------------------------------------<br />
 */
public abstract class BaseFragment extends SupportFragment {

    private String mFragmentName;
    private Unbinder unbinder;
    private boolean mViewDestroyed = true;

    public BaseFragment() {
        mFragmentName = this.getClass().getName();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view, savedInstanceState);
        mViewDestroyed = false;
    }

    /**
     * 设置Fragment Layout
     */
    protected abstract int getLayoutId();

    /**
     * 初始化视图
     */
    protected abstract void initView(View view, @Nullable Bundle savedInstanceState);

    @CallSuper
    @Override
    public void onDestroyView() {
        if (unbinder != null) {
            unbinder.unbind();
        }
        mViewDestroyed = true;
        super.onDestroyView();
    }

    /**
     * Detect whether the view of this {@link Fragment} is destroyed.
     *
     * @return Is the view destroyed.
     */
    public boolean isViewDestroyed() {
        return mViewDestroyed;
    }

    /**
     * 返回当前Fragment 模块名字
     * <p>该名称用于不同模块的标识，默认为ClassName</p>
     *
     * @return
     */
    @NonNull
    protected String getFragmentName() {
        return mFragmentName;
    }

}
